using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace SurveyHost.Core.DAL
{
	/// <summary>
	/// Strongly-typed collection for the Measure class.
	/// </summary>
	[Serializable]
	public partial class MeasureCollection : ActiveList<Measure, MeasureCollection> 
	{	   
		public MeasureCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Measure table.
	/// </summary>
	[Serializable]
	public partial class Measure : ActiveRecord<Measure>
	{
		#region .ctors and Default Settings
		
		public Measure()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Measure(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Measure(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Measure(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Measure", TableType.Table, DataService.GetInstance("SurveyHost"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarMeasureName = new TableSchema.TableColumn(schema);
				colvarMeasureName.ColumnName = "MeasureName";
				colvarMeasureName.DataType = DbType.String;
				colvarMeasureName.MaxLength = 100;
				colvarMeasureName.AutoIncrement = false;
				colvarMeasureName.IsNullable = false;
				colvarMeasureName.IsPrimaryKey = true;
				colvarMeasureName.IsForeignKey = false;
				colvarMeasureName.IsReadOnly = false;
				colvarMeasureName.DefaultSetting = @"";
				colvarMeasureName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMeasureName);
				
				TableSchema.TableColumn colvarRank = new TableSchema.TableColumn(schema);
				colvarRank.ColumnName = "Rank";
				colvarRank.DataType = DbType.Int32;
				colvarRank.MaxLength = 0;
				colvarRank.AutoIncrement = false;
				colvarRank.IsNullable = false;
				colvarRank.IsPrimaryKey = false;
				colvarRank.IsForeignKey = false;
				colvarRank.IsReadOnly = false;
				
						colvarRank.DefaultSetting = @"((0))";
				colvarRank.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRank);
				
				TableSchema.TableColumn colvarDefaultAnswerTypeName = new TableSchema.TableColumn(schema);
				colvarDefaultAnswerTypeName.ColumnName = "DefaultAnswerTypeName";
				colvarDefaultAnswerTypeName.DataType = DbType.String;
				colvarDefaultAnswerTypeName.MaxLength = 100;
				colvarDefaultAnswerTypeName.AutoIncrement = false;
				colvarDefaultAnswerTypeName.IsNullable = true;
				colvarDefaultAnswerTypeName.IsPrimaryKey = false;
				colvarDefaultAnswerTypeName.IsForeignKey = true;
				colvarDefaultAnswerTypeName.IsReadOnly = false;
				colvarDefaultAnswerTypeName.DefaultSetting = @"";
				
					colvarDefaultAnswerTypeName.ForeignKeyTableName = "AnswerObjectType";
				schema.Columns.Add(colvarDefaultAnswerTypeName);
				
				TableSchema.TableColumn colvarInstructions = new TableSchema.TableColumn(schema);
				colvarInstructions.ColumnName = "Instructions";
				colvarInstructions.DataType = DbType.String;
				colvarInstructions.MaxLength = 2000;
				colvarInstructions.AutoIncrement = false;
				colvarInstructions.IsNullable = true;
				colvarInstructions.IsPrimaryKey = false;
				colvarInstructions.IsForeignKey = false;
				colvarInstructions.IsReadOnly = false;
				colvarInstructions.DefaultSetting = @"";
				colvarInstructions.ForeignKeyTableName = "";
				schema.Columns.Add(colvarInstructions);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["SurveyHost"].AddSchema("Measure",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("MeasureName")]
		public string MeasureName 
		{
			get { return GetColumnValue<string>("MeasureName"); }

			set { SetColumnValue("MeasureName", value); }

		}

		  
		[XmlAttribute("Rank")]
		public int Rank 
		{
			get { return GetColumnValue<int>("Rank"); }

			set { SetColumnValue("Rank", value); }

		}

		  
		[XmlAttribute("DefaultAnswerTypeName")]
		public string DefaultAnswerTypeName 
		{
			get { return GetColumnValue<string>("DefaultAnswerTypeName"); }

			set { SetColumnValue("DefaultAnswerTypeName", value); }

		}

		  
		[XmlAttribute("Instructions")]
		public string Instructions 
		{
			get { return GetColumnValue<string>("Instructions"); }

			set { SetColumnValue("Instructions", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
				
		private SurveyHost.Core.DAL.StemCollection colStemRecords;
		public SurveyHost.Core.DAL.StemCollection StemRecords
		{
			get
			{
				if(colStemRecords == null)
					colStemRecords = new SurveyHost.Core.DAL.StemCollection().Where(Stem.Columns.MeasureName, MeasureName).Load();
				return colStemRecords;			
			}

			set { colStemRecords = value; }

		}

		
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a AnswerObjectType ActiveRecord object related to this Measure
		/// 
		/// </summary>
		public SurveyHost.Core.DAL.AnswerObjectType AnswerObjectType
		{
			get { return SurveyHost.Core.DAL.AnswerObjectType.FetchByID(this.DefaultAnswerTypeName); }

			set { SetColumnValue("DefaultAnswerTypeName", value.AnswerTypeName); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varMeasureName,int varRank,string varDefaultAnswerTypeName,string varInstructions)
		{
			Measure item = new Measure();
			
			item.MeasureName = varMeasureName;
			
			item.Rank = varRank;
			
			item.DefaultAnswerTypeName = varDefaultAnswerTypeName;
			
			item.Instructions = varInstructions;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(string varMeasureName,int varRank,string varDefaultAnswerTypeName,string varInstructions)
		{
			Measure item = new Measure();
			
				item.MeasureName = varMeasureName;
				
				item.Rank = varRank;
				
				item.DefaultAnswerTypeName = varDefaultAnswerTypeName;
				
				item.Instructions = varInstructions;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string MeasureName = @"MeasureName";
			 public static string Rank = @"Rank";
			 public static string DefaultAnswerTypeName = @"DefaultAnswerTypeName";
			 public static string Instructions = @"Instructions";
						
		}

		#endregion
	}

}

